#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent), sim(NULL), type(0), ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->reinitialize->hide();
    connectAll();
    connect(ui->launchButton, SIGNAL(clicked()),this, SLOT(launch()));
    connect(ui->simuButton, SIGNAL(clicked()),this, SLOT(launch()));
    connect(ui->tabWidget, SIGNAL(currentChanged(int)), this, SLOT(ontab(int)));
    connect(ui->reinitialize, SIGNAL(clicked()), this, SLOT(reinitializeSimu()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::ontab(int i)
{
    tab = i;
}


void MainWindow::reinitializeSimu()
{
    sim->reinitialize();
}

void MainWindow::launch()
{
    int tab = ui->tabWidget->currentIndex();

    if(tab == 0)
    {
        QString file = QFileDialog::getSaveFileName(this, "Enregistrer un fichier", QDir::homePath(), "Reinforcement Learning Data (*.rld)" );

        if(file == NULL || file == "")
        {
            return;
        }
        if(file.section('.',1) != "rld"){
            file.append(".rld");
        }
        filename = file;
        learner = new Learner<2>(this);
        learner->start();

        connect(learner , SIGNAL(finished()) , this , SLOT(learnFinished()));


    }
    else
    {
        QString file = QFileDialog::getOpenFileName(this, "Ouvrir un fichier", QDir::homePath(), "Reinforcement Learning Data (*.rld)");

        if(file == NULL)
        {
            return;
        }
        if(ui->reinitialize->isVisible()){
            qDebug() << "Delete sim";
            delete sim;
            delete ui->visualizer->layout();
        }

        sim = new Simulator<2>(file);
        QVBoxLayout *l = new QVBoxLayout;
        l->addWidget(sim);
        ui->visualizer->setLayout(l);
        ui->reinitialize->setVisible(true);
        sim->start();
    }
}

void MainWindow::learnFinished(){
    setDisabled(false);
    QMessageBox::information(this, "Apprentissage terminé", "L'apprentissage est terminé!");
}

void MainWindow::readInput()
{
    physParameters.clear();
    motParameters.clear();
    senParameters.clear();

    type = ui->typeCombo->currentIndex();

    switch(type)
    {
    case 0:
        physParameters.append(ui->pb11->value());
        physParameters.append(ui->pb12->value());
        physParameters.append(ui->pb13->value());
        break;
    case 1:
        physParameters.append(ui->pb21->value());
        physParameters.append(ui->pb22->value());
        break;
    case 2:
        physParameters.append(ui->pb31->value());
        physParameters.append(ui->pb32->value());
        physParameters.append(ui->pb33->value());
        physParameters.append(ui->pb34->value());
        physParameters.append(ui->pb35->value());
        physParameters.append(ui->pb36->value());
        break;
    default:
        physParameters.append(ui->pb41->value());
        physParameters.append(ui->pb42->value());
        physParameters.append(ui->pb43->value());
        physParameters.append(ui->pb44->value());
        physParameters.append(ui->pb45->value());
        break;
    }

    motParameters.append(ui->mb11->value());
    motParameters.append(ui->mb12->value());
    motParameters.append(ui->mb13->value());
    motParameters.append(ui->mb14->value());

    senParameters.append(ui->sb11->value());
    senParameters.append(ui->sb12->value());
    senParameters.append(ui->sb13->value());

}

void MainWindow::pb11(double d)
{
    ui->ps11->setValue(static_cast<int>(d*10));
}

void MainWindow::ps11(int i)
{
    ui->pb11->setValue(static_cast<double>(i)/10);
}

void MainWindow::pb12(double d)
{
    ui->ps12->setValue(static_cast<int>(d*10));
}

void MainWindow::ps12(int i)
{
    ui->pb12->setValue(static_cast<double>(i)/10);
}

void MainWindow::pb13(double d)
{
    ui->ps13->setValue(static_cast<int>(d*10));
}

void MainWindow::ps13(int i)
{
    ui->pb13->setValue(static_cast<double>(i)/10);
}

void MainWindow::pb21(double d)
{
    ui->ps21->setValue(static_cast<int>(d*10));
}

void MainWindow::ps21(int i)
{
    ui->pb21->setValue(static_cast<double>(i)/10);
}

void MainWindow::pb22(double d)
{
    ui->ps22->setValue(static_cast<int>(d*10));
}

void MainWindow::ps22(int i)
{
    ui->pb22->setValue(static_cast<double>(i)/10);
}

void MainWindow::pb31(double d)
{
    ui->ps31->setValue(static_cast<int>(d*10));
}

void MainWindow::ps31(int i)
{
    ui->pb31->setValue(static_cast<double>(i)/10);
}

void MainWindow::pb32(double d)
{
    ui->ps32->setValue(static_cast<int>(d*10));
}

void MainWindow::ps32(int i)
{
    ui->pb32->setValue(static_cast<double>(i)/10);
}

void MainWindow::pb33(double d)
{
    ui->ps33->setValue(static_cast<int>(d*10));
}

void MainWindow::ps33(int i)
{
    ui->pb33->setValue(static_cast<double>(i)/10);
}

void MainWindow::pb34(double d)
{
    ui->ps34->setValue(static_cast<int>(d*10));
}

void MainWindow::ps34(int i)
{
    ui->pb34->setValue(static_cast<double>(i)/10);
}

void MainWindow::pb35(double d)
{
    ui->ps35->setValue(static_cast<int>(d*10));
}

void MainWindow::ps35(int i)
{
    ui->pb35->setValue(static_cast<double>(i)/10);
}

void MainWindow::pb36(double d)
{
    ui->ps36->setValue(static_cast<int>(d*10));
}

void MainWindow::ps36(int i)
{
    ui->pb36->setValue(static_cast<double>(i)/10);
}

void MainWindow::pb41(double d)
{
    ui->ps41->setValue(static_cast<int>(d*10));
}

void MainWindow::ps41(int i)
{
    ui->pb41->setValue(static_cast<double>(i)/10);
}

void MainWindow::pb42(double d)
{
    ui->ps42->setValue(static_cast<int>(d*10));
}

void MainWindow::ps42(int i)
{
    ui->pb42->setValue(static_cast<double>(i)/10);
}

void MainWindow::pb43(double d)
{
    ui->ps43->setValue(static_cast<int>(d*10));
}

void MainWindow::ps43(int i)
{
    ui->pb43->setValue(static_cast<double>(i)/10);
}

void MainWindow::pb44(double d)
{
    ui->ps44->setValue(static_cast<int>(d*10));
}

void MainWindow::ps44(int i)
{
    ui->pb44->setValue(static_cast<double>(i)/10);
}

void MainWindow::pb45(double d)
{
    ui->ps45->setValue(static_cast<int>(d*10));
}

void MainWindow::ps45(int i)
{
    ui->pb45->setValue(static_cast<double>(i)/10);
}

void MainWindow::mb12(double d)
{
    ui->ms12->setValue(static_cast<int>(d*1000));
}

void MainWindow::ms12(int i)
{
    ui->mb12->setValue(static_cast<double>(i)/1000);
}

void MainWindow::mb13(double d)
{
    ui->ms13->setValue(static_cast<int>(d*1000));
}

void MainWindow::ms13(int i)
{
    ui->mb13->setValue(static_cast<double>(i)/1000);
}

void MainWindow::mb14(double d)
{
    ui->ms14->setValue(static_cast<int>(d*10));
}

void MainWindow::ms14(int i)
{
    ui->mb14->setValue(static_cast<double>(i)/10);
}

void MainWindow::sb12(double d)
{
    ui->ss12->setValue(static_cast<int>(d*1000));
}

void MainWindow::ss12(int i)
{
    ui->sb12->setValue(static_cast<double>(i)/1000);
}

void MainWindow::connectAll()
{
    connect(ui->pb11, SIGNAL(valueChanged(double)), this, SLOT(pb11(double)));
    connect(ui->ps11, SIGNAL(sliderMoved(int)), this, SLOT(ps11(int)));

    connect(ui->pb12, SIGNAL(valueChanged(double)), this, SLOT(pb12(double)));
    connect(ui->ps12, SIGNAL(sliderMoved(int)), this, SLOT(ps12(int)));

    connect(ui->pb13, SIGNAL(valueChanged(double)), this, SLOT(pb13(double)));
    connect(ui->ps13, SIGNAL(sliderMoved(int)), this, SLOT(ps13(int)));

    connect(ui->pb21, SIGNAL(valueChanged(double)), this, SLOT(pb21(double)));
    connect(ui->ps21, SIGNAL(sliderMoved(int)), this, SLOT(ps21(int)));

    connect(ui->pb22, SIGNAL(valueChanged(double)), this, SLOT(pb22(double)));
    connect(ui->ps22, SIGNAL(sliderMoved(int)), this, SLOT(ps22(int)));

    connect(ui->pb31, SIGNAL(valueChanged(double)), this, SLOT(pb31(double)));
    connect(ui->ps31, SIGNAL(sliderMoved(int)), this, SLOT(ps31(int)));

    connect(ui->pb32, SIGNAL(valueChanged(double)), this, SLOT(pb32(double)));
    connect(ui->ps32, SIGNAL(sliderMoved(int)), this, SLOT(ps32(int)));

    connect(ui->pb33, SIGNAL(valueChanged(double)), this, SLOT(pb33(double)));
    connect(ui->ps33, SIGNAL(sliderMoved(int)), this, SLOT(ps33(int)));

    connect(ui->pb34, SIGNAL(valueChanged(double)), this, SLOT(pb34(double)));
    connect(ui->ps34, SIGNAL(sliderMoved(int)), this, SLOT(ps34(int)));

    connect(ui->pb35, SIGNAL(valueChanged(double)), this, SLOT(pb35(double)));
    connect(ui->ps35, SIGNAL(sliderMoved(int)), this, SLOT(ps35(int)));

    connect(ui->pb36, SIGNAL(valueChanged(double)), this, SLOT(pb36(double)));
    connect(ui->ps36, SIGNAL(sliderMoved(int)), this, SLOT(ps36(int)));

    connect(ui->pb41, SIGNAL(valueChanged(double)), this, SLOT(pb41(double)));
    connect(ui->ps41, SIGNAL(sliderMoved(int)), this, SLOT(ps41(int)));

    connect(ui->pb42, SIGNAL(valueChanged(double)), this, SLOT(pb42(double)));
    connect(ui->ps42, SIGNAL(sliderMoved(int)), this, SLOT(ps42(int)));

    connect(ui->pb43, SIGNAL(valueChanged(double)), this, SLOT(pb43(double)));
    connect(ui->ps43, SIGNAL(sliderMoved(int)), this, SLOT(ps43(int)));

    connect(ui->pb44, SIGNAL(valueChanged(double)), this, SLOT(pb44(double)));
    connect(ui->ps44, SIGNAL(sliderMoved(int)), this, SLOT(ps44(int)));

    connect(ui->pb45, SIGNAL(valueChanged(double)), this, SLOT(pb45(double)));
    connect(ui->ps45, SIGNAL(sliderMoved(int)), this, SLOT(ps45(int)));


    connect(ui->mb12, SIGNAL(valueChanged(double)), this, SLOT(mb12(double)));
    connect(ui->ms12, SIGNAL(sliderMoved(int)), this, SLOT(ms12(int)));

    connect(ui->mb13, SIGNAL(valueChanged(double)), this, SLOT(mb13(double)));
    connect(ui->ms13, SIGNAL(sliderMoved(int)), this, SLOT(ms13(int)));

    connect(ui->mb14, SIGNAL(valueChanged(double)), this, SLOT(mb14(double)));
    connect(ui->ms14, SIGNAL(sliderMoved(int)), this, SLOT(ms14(int)));

    connect(ui->sb12, SIGNAL(valueChanged(double)), this, SLOT(sb12(double)));
    connect(ui->ss12, SIGNAL(sliderMoved(int)), this, SLOT(ss12(int)));
}
